<template>
  <div class="home">
    <div class="title-container">
      <div class="search-content">
        <el-form :model="searchModel" :inline="true">
          <el-form-item>
            <el-input
              v-model="searchModel.userName"
              placeholder="请输入名称"
              prefix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="search-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchClick"
            size="medium"
          >搜索
          </el-button>
        </div>
      </div>
    </div>

    <el-card>
      <div class="addAccount">
        <perButton label="系统新建" size="medium" icon="el-icon-plus" perms="PC_SYS_BULD_ADD" type="primary"
                   @click="handlerAdd"></perButton>
        <el-button
          type="success"
          icon="el-icon-refresh"
          size="medium"
          @click="refresh"
        >刷新
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%;"
        class="elTable"
      >
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <el-table-column prop="systemCode" label="系统编码" align="center"/>
        <el-table-column prop="systemName" label="系统名称" align="center"/>
        <el-table-column prop="systemDes" label="系统描述" show-overflow-tooltip align="center"/>
        <el-table-column prop="systemRefCode" label="系统识别码" align="center"/>
        <el-table-column prop="createByName" label="创建人" align="center"/>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="350" align="center">
          <template #default="{ row }">
            <!-- <perButton label="配置登录" type="primary" size="mini" perms="PC_SYS_BULD_LOGIN" @click="settingLogin(row)"></perButton> -->
<!--            <perButton label="配置顶部" type="primary" size="mini" perms="PC_SYS_BULD_TOP"-->
<!--                       @click="settingTop(row)"></perButton>-->
            <perButton label="配置菜单" type="primary" size="mini" perms="PC_SYS_BULD_MENU"
                       @click="settingMenu(row)"></perButton>
            <perButton label="访问" type="success" size="mini" perms="PC_SYS_BULD_ACCESS"
                       @click="accessing(row)"></perButton>
            <perButton label="编辑" type="warning" size="mini" perms="PT_USER_EDIT"
                       @click="handlerEdit(row)"></perButton>
            <perButton label="删除" type="danger" size="mini" perms="PT_USER_DEL"
                       @click="handlerDelete(row)"></perButton>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        class="pagination"
        :total="count"
        :page.sync="pageIndex"
        :limit.sync="pageSize"
        @pagination="getAccountData"
      />
    </el-card>

    <!-- 系统新建弹框 -->
    <yc-dialog ref="addDialog" :centerTitle="centerTitle" :dialogTitle="dialogTitle">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="addFormRef"
            :model="addForm"
            :rules="rules"
            label-width="100px"
          >
            <el-form-item label="系统名称" prop="systemName">
              <el-input
                v-model="addForm.systemName"
              />
            </el-form-item>
            <el-form-item label="系统识别码" prop="systemRefCode">
              <el-input
                v-model="addForm.systemRefCode"
              />
            </el-form-item>
            <el-form-item label="系统描述">
              <el-input
                v-model="addForm.systemDes"
                type="textarea" :rows="2"
              />
            </el-form-item>
            <el-form-item label="登录标题" prop="loginTitle">
              <el-input
                v-model="addForm.loginTitle"
              />
            </el-form-item>
            <el-form-item label="选择底图" prop="loginBackgroundUrl">
              <div class="col-center" v-if="addForm.loginBackgroundUrl">
                <img
                  :src="addForm.loginBackgroundUrl"
                  class="bgcss mr-3"
                />
                <el-button icon="el-icon-edit" @click="selectPic('bg')">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic('bg')" v-else>选择照片</el-button>
            </el-form-item>
            <el-form-item label="顶部标题" prop="headTitle">
              <el-input
                v-model="addForm.headTitle"
              />
            </el-form-item>
            <el-form-item label="顶部ICON" prop="headIcon">
              <div class="col-center" v-if="addForm.headIcon">
                <img :src="addForm.headIcon" class="bgcss mr-3"/>
                <el-button icon="el-icon-edit" @click="selectPic('top')">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic('top')" v-else>选择照片</el-button>
            </el-form-item>
            <el-form-item label="系统风格" prop="systemUiType">
              <div class="d-flex">
                <div class="d-flex align-items-center flex-column mr-3">
                  <img class="bgcss mb-2" src="@/assets/systemBuilding/white.png" @click="addForm.systemUiType = 1"/>
                  <el-radio :label="1" v-model="addForm.systemUiType">纯白风</el-radio>
                </div>
                <div class="d-flex align-items-center flex-column">
                  <img class="bgcss mb-2" src="@/assets/systemBuilding/black.png" @click="addForm.systemUiType = 0"/>
                  <el-radio :label="0" v-model="addForm.systemUiType">暗色风</el-radio>
                </div>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addLoading" @click="addSave">确定</el-button>
        <el-button @click="$refs.addDialog.dialogVisible = false">取消
        </el-button>
      </template>
    </yc-dialog>

    <!-- 选择照片 -->
    <yc-dialog ref="iconDialog" :show-center="false" :dialogTitle="dialogTitle2">
      <template #content>
        <div class="dialog-content2">
          <div class="d-flex flex-wrap">
            <div class="d-flex align-items-center flex-column mr-4 mb-4" v-for="(item ,index) in bgList" :key="index">
              <img :src="item.picUrl" class="bgcss mb-2" @click="bgUrl = item.picUrl"/>
              <el-radio :label="item.picUrl" v-model="bgUrl">{{ item.picName }}</el-radio>
            </div>
          </div>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" @click="iconSave">确定</el-button>
        <el-button @click="$refs.iconDialog.dialogVisible = false">取消</el-button>
      </template>
    </yc-dialog>

    <!-- 配置顶部弹框 -->
    <yc-dialog ref="topDialog" :show-center="false" :dialogTitle="dialogTitle">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="topFormRef"
            :model="topForm"
            :rules="topRules"
            label-width="90px"
          >
            <el-form-item label="顶部标题" prop="topTitle">
              <el-input
                v-model="topForm.topTitle"
              />
            </el-form-item>
            <el-form-item label="顶部icon" prop="topIcon">
              <div class="col-center" v-if="topForm.topIcon">
                <img
                  :src="topForm.topIcon"
                  class="bgcss mr-3"
                />
                <el-button icon="el-icon-edit" @click="selectPic">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic" v-else></el-button>
            </el-form-item>
            <el-form-item label="区域一" prop="erea1">
              <div class="col-center" v-if="topForm.erea1">
                <!-- <img
                  :src="topForm.erea1"
                  class="bgcss mr-3"
                /> -->
                <el-button icon="el-icon-edit" @click="selectPic">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic" v-else></el-button>
            </el-form-item>
            <el-form-item label="区域二" prop="erea2">
              <div class="col-center" v-if="topForm.erea2">
                <!-- <img
                  :src="topForm.erea2"
                  class="bgcss mr-3"
                /> -->
                <el-button icon="el-icon-edit" @click="selectPic">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic" v-else></el-button>
            </el-form-item>
            <el-form-item label="区域三" prop="erea3">
              <div class="col-center" v-if="topForm.erea3">
                <!-- <img
                  :src="topForm.erea3"
                  class="bgcss mr-3"
                /> -->
                <el-button icon="el-icon-edit" @click="selectPic">重新选择</el-button>
              </div>
              <el-button icon="el-icon-plus" @click="selectPic" v-else></el-button>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="topLoading" @click="topSave">确定</el-button>
        <el-button @click="$refs.topDialog.dialogVisible = false">取消
        </el-button>
      </template>
    </yc-dialog>
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"
  import {
    queryPage,
    getBasePic,
    insertByModel,
    updateByModel,
    deleteByModel
  } from "@/api/systemBuilding/pcSystemBuilding"

  export default {
    components: {
      ycDialog,
      perButton
    },
    data() {
      return {
        tableData: [],
        count: 0,
        pageIndex: 1,
        pageSize: 10,
        loading: false,
        searchModel: {
          userName: "",
        },

        centerTitle: "",
        dialogTitle: "",
        dialogTitle2: "",
        addLoading: false,

        addForm: {
          id: 0,
          systemName: "",
          systemRefCode: "",
          systemDes: "",
          loginTitle: "",
          loginBackgroundUrl: "",
          headTitle: "",
          headIcon: "",
          systemUiType: 1,// 系统风格 0暗色风格 1白色风格
        },
        rules: {
          systemName: [
            {required: true, message: "请输入系统名称", trigger: "blur"}
          ],
          systemRefCode: [
            {required: true, message: "请输入系统识别码", trigger: "blur"}
          ],
          loginTitle: [
            {required: true, message: "请输入登录标题", trigger: "blur"}
          ],
          loginBackgroundUrl: [
            {required: true, message: "请选择底图", trigger: "blur"}
          ],
          headTitle: [
            {required: true, message: "请输入登录标题", trigger: "blur"}
          ],
          headIcon: [
            {required: true, message: "请选择底图", trigger: "blur"}
          ],
          systemUiType: [
            {required: true, message: "请选择系统风格", trigger: "blur"}
          ]
        },
        customerData: [],
        bgList: [],
        picType: "",
        bgUrl: "",

        topForm: {
          id: "",
          topTitle: "",
          topIcon: "",
          erea1: "",
          erea2: "",
          erea3: ""
        },
        topRules: {},
        topLoading: false,
      }
    },
    created() {
      this.getAccountData()
    },
    methods: {
      /**
       * 获取表格方法
       */
      async getAccountData(tableInfo) {
        this.loading = true
        const {object} = await queryPage({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        })
        this.tableData = object.data
        this.count = object.count
        this.loading = false
      },

      pagination(pageItem) {
        this.getAccountData()
      },

      // 搜索按钮
      async searchClick() {
        this.getAccountData(this.searchModel)
      },

      /**
       * 删除按钮
       */
      async handlerDelete({id, userCode}) {
        this.$confirm("确定删除此数据吗", "删除操作", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          center: true
        }).then(async () => {
          const {message, status} = await deleteByModel({id, userCode})
          if (status == "200") {
            this.getAccountData()
            this.isGlobalMessageShow = true
            this.message = "success"
          } else {
            this.message = "error"
          }
        })
      },
      /**
       * 刷新
       */
      refresh() {
        this.searchModel = {
          userName: "",
        }
        this.getAccountData()
      },

      /**
       * 系统新建
       */
      handlerAdd() {
        this.dialogTitle = "系统新建"
        this.centerTitle = "系统新建"

        this.addLoading = false;
        this.$refs.addDialog.dialogVisible = true

        Object.keys(this.addForm).forEach((item) => {
          this.addForm[item] = ""
        })
        this.addForm.systemUiType = 1;
      },
      /**
       * 修改
       */
      handlerEdit(row) {
        this.dialogTitle = "系统编辑"
        this.centerTitle = "系统编辑"

        this.addForm.id = row.id
        this.addForm.systemName = row.systemName
        this.addForm.systemRefCode = row.systemRefCode
        this.addForm.systemDes = row.systemDes
        this.addForm.loginTitle = row.loginTitle
        this.addForm.loginBackgroundUrl = row.loginBackgroundUrl
        this.addForm.headTitle = row.headTitle
        this.addForm.headIcon = row.headIcon
        this.addForm.systemUiType = row.systemUiType

        this.addLoading = false;
        this.$refs.addDialog.dialogVisible = true
      },
      /**
       * 系统新建保存
       */
      addSave() {
        this.$refs.addFormRef.validate(async (v) => {
          if (v) {
            this.addLoading = true;

            if (this.addForm.id) {
              const {
                id,
                systemName,
                systemRefCode,
                systemDes,
                loginTitle,
                loginBackgroundUrl,
                headTitle,
                headIcon,
                systemUiType
              } = this.addForm

              const {message, status} = await updateByModel({
                systemName,
                systemRefCode,
                systemDes,
                loginTitle,
                loginBackgroundUrl,
                headTitle,
                headIcon,
                systemUiType,
                id
              })
              if (status == 200) {
                this.getAccountData()
                this.$refs.addDialog.dialogVisible = false
                this.$message.success(message)
              }
            } else {
              const {
                systemName,
                systemRefCode,
                systemDes,
                loginTitle,
                loginBackgroundUrl,
                headTitle,
                headIcon,
                systemUiType
              } = this.addForm
              const {message, status} = await insertByModel({
                systemName,
                systemRefCode,
                systemDes,
                loginTitle,
                loginBackgroundUrl,
                headTitle,
                headIcon,
                systemUiType
              });
              if (status == 200) {
                this.getAccountData()
                this.$refs.addDialog.dialogVisible = false
                this.$message.success(message)
              }
            }
            this.addLoading = false;
          }
        })
      },

      getBasePicList() {
        getBasePic({picType: 0}).then(res => {
          if (res.status == 200) {
            this.bgList = res.object;
          }
        })
      },
      getBasePicList2() {
        getBasePic({picType: 1}).then(res => {
          if (res.status == 200) {
            this.bgList = res.object;
          }
        })
      },
      /**
       * 选择图片
       */
      selectPic(val) {
        this.dialogTitle2 = "选择图片"
        this.bgUrl = ""
        this.picType = val;
        if (val == 'bg') {
          this.getBasePicList()
        } else {
          this.getBasePicList2()
        }
        this.$refs.iconDialog.dialogVisible = true
      },

      /**
       * 选择图片保存
       */
      iconSave() {
        if (this.picType == 'bg') {
          this.addForm.loginBackgroundUrl = this.bgUrl;
        } else if (this.picType == 'top') {
          this.addForm.headIcon = this.bgUrl;
        }

        this.$refs.iconDialog.dialogVisible = false
      },

      /**
       * 配置顶部
       */
      settingTop(row) {
        this.dialogTitle2 = "配置顶部"
        this.$refs.topDialog.dialogVisible = true;

        Object.keys(this.topForm).forEach((item) => {
          this.topForm[item] = ""
        })
      },

      /**
       * 配置顶部保存
       */
      topSave() {

      },

      /**
       * 配置菜单
       */
      settingMenu(row) {
        this.$router.push({
          path: "/systemBuilding/pcSystemBuildMenu",
          query: {
            systemCode: row.systemCode
          }
        })
      },

      /**
       * 分配人员
       */
      distributePeople(row) {
        this.$router.push({
          path: "/systemBuilding/settingPeople",
          query: {
            systemCode: row.systemCode
          }
        })
      },

      /**
       * 访问
       */
      accessing(row) {
        window.open(window.CLIENT_URL + "/#/login?systemRefCode=" + row.systemRefCode);
      },
    }
  }
</script>

<style lang="scss" scoped>
  .title-container {
    display: flex;
    justify-content: flex-end;
    height: 60px;

    .search-content {
      margin-left: 20px;
      display: flex;
    }

    .addAccount {
      margin: 10px 20px;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
  }

  .footer {
    height: 360px;
    display: flex;
    align-items: flex-end;
    padding: 5px 20px;
  }

  .bgcss {
    width: 160px;
    height: 90px;
    cursor: pointer;
  }

  .dialog-content {
    .el-radio {
      display: flex;
      flex-direction: column-reverse;
      align-items: center;
      justify-items: center;

      ::v-deep.el-radio__input {
        display: flex;
        align-items: center;
        justify-items: center;
        margin-top: 8px;
      }
    }
  }

  .dialog-content2 {
    .el-radio {
      display: flex;
      flex-direction: column-reverse;
      align-items: center;

      ::v-deep.el-radio__label {
        margin-bottom: 5px;
      }
    }
  }
</style>

